package com.example.demo.topic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.example.demo.topic.entity.TNoteManage;
import com.example.demo.topic.entity.TTopic;
import com.example.demo.topic.mapper.TNoteManageMapper;
import com.example.demo.topic.service.ITTopicService;
import com.example.demo.topic.service.TNoteManageService;
import com.example.demo.topic.vo.TNoteManageVo;
import com.example.demo.util.result.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 发帖管理表; 服务实现类
 * </p>
 *
 * @author 涛
 * @since 2024-08-01
 */
@Service("tNoteManageService")
public class TNoteManageServiceImpl extends ServiceImpl<TNoteManageMapper, TNoteManage> implements TNoteManageService {

    @Autowired
    private TNoteManageMapper tNoteManageMapper;

    @Autowired
    private ITTopicService tTopicService;

    @Override
    public Result<?> getNoteList(Page<TNoteManageVo> page, TNoteManageVo note) {
        QueryWrapper<TNoteManageVo> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(note.getTitle())){
            wrapper.eq("title",note.getTitle());
        }
        if(StringUtils.isNotEmpty(note.getMasterId())){
            wrapper.eq("masterId",note.getMasterId());
        }
        if(StringUtils.isNotEmpty(note.getAccount())){
            wrapper.eq("account",note.getAccount());
        }
        if(StringUtils.isNotEmpty(note.getType())){
            wrapper.eq("type",note.getType());
        }
        if(note.getStartTime()!=null && note.getEndTime()!=null){
            wrapper.between("createdTime",note.getStartTime(),note.getEndTime());
        }
        return Result.ok(tNoteManageMapper.getNoteList(page,wrapper));
    }

    @Override
    public Result<?> viewNote(Integer id,Integer uid) {
        TNoteManageVo tNoteManageVo = tNoteManageMapper.viewNote(id,uid);

        //获取话题列表
//        QueryWrapper<TTopic> queryWrapper = new QueryWrapper<>();
//        if(StringUtils.isNotEmpty(tNoteManageVo.getTopic())){
//            Object[] params = tNoteManageVo.getTopic().split(",");
//            queryWrapper.in("topic_code", params);
//            queryWrapper.select("GROUP_CONCAT(TOPIC_NAME SEPARATOR ',') AS topicName");
//            TTopic topic = tTopicService.getOne(queryWrapper);
//            tNoteManageVo.setTopicText(topic.getTopicName());
//        }

        return Result.ok(tNoteManageVo);
    }

    @Override
    public Result<?> getNoteByAccount(Page<TNoteManageVo> page, String code, Integer uid) {
        return Result.ok(tNoteManageMapper.getNoteByAccount(page,code,uid));
    }

    @Override
    public Result<?> getAccountLikes(String code) {
        return Result.ok(tNoteManageMapper.getAccountLikes(code));
    }

    @Override
    public Result<?> getCollectNote(Page<TNoteManageVo> page, Integer id) {
        return Result.ok(tNoteManageMapper.getCollectNote(page,id));
    }
}
